﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Poco;

namespace Metier
{
    public class GestionManager
    {
        public void Ajouter(Manager manager)
        {
            try
            {
                using (Context db = new Context())
                {
                    db.Utilisateurs.AddObject(manager);
                    db.SaveChanges();
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public Manager Chercher(int managerId)
        {
            try
            {
                using (Context db = new Context())
                {
                    return db.Utilisateurs.Where(m => m.Id == managerId).OfType<Manager>().FirstOrDefault();
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public List<Manager> Lister()
        {
            try
            {
                using (Context db = new Context())
                {
                    return db.Utilisateurs.OfType<Manager>().ToList();
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public Manager Chercher(string email)
        {
            try
            {
                using (Context db = new Context())
                {
                    return (from m in db.Utilisateurs
                            where m.Email == email
                            select m).OfType<Manager>().FirstOrDefault();
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public Manager Chercher(Restaurant restaurant)
        {
            try
            {
                using (Context db = new Context())
                {
                    return (from m in db.Utilisateurs.OfType<Manager>()
                            where m.Restaurant.Id == restaurant.Id
                            select m).FirstOrDefault();
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void MettreAJour(Manager manager)
        {
            try
            {
                using (Context db = new Context())
                {
                    db.Utilisateurs.Attach(manager);
                    db.ObjectStateManager.ChangeObjectState(manager, System.Data.EntityState.Modified);
                    db.SaveChanges();
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void Supprimer(Manager manager)
        {
            try
            {
                using (Context db = new Context())
                {
                    db.Utilisateurs.DeleteObject(manager);
                    db.SaveChanges();
                }
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
